/*
 * <summary></summary>
 * <author>He Han</author>
 * <email>hankcs.cn@gmail.com</email>
 * <create-date>2015/1/29 19:07</create-date>
 *
 * <copyright file="LdaUtil.java" company="上海林原信息科技有限公司">
 * Copyright (c) 2003-2014, 上海林原信息科技有限公司. All Right Reserved, http://www.linrunsoft.com/
 * This source is subject to the LinrunSpace License. Please contact 上海林原信息科技有限公司 to get more information.
 * </copyright>
 */
package com.lda;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.my.crawler.MyConfig;

/**
 * @author hankcs
 */
public class LdaUtil {
	/**
	 * To translate a LDA matrix to readable result
	 * 
	 * @param phi
	 *            the LDA model
	 * @param vocabulary
	 * @param limit
	 *            limit of max words in a topic
	 * @return a map array
	 */
	public static Map<String, Double>[] translate(double[][] phi,
			Vocabulary vocabulary, int limit) {
		limit = Math.min(limit, phi[0].length);
		Map<String, Double>[] result = new Map[phi.length];
		for (int k = 0; k < phi.length; k++) {
			Map<Double, String> rankMap = new TreeMap<Double, String>(
					Collections.reverseOrder());
			for (int i = 0; i < phi[k].length; i++) {
				rankMap.put(phi[k][i], vocabulary.getWord(i));
			}
			Iterator<Map.Entry<Double, String>> iterator = rankMap.entrySet()
					.iterator();
			result[k] = new LinkedHashMap<String, Double>();
			for (int i = 0; i < limit; ++i) {
				Map.Entry<Double, String> entry = iterator.next();
				result[k].put(entry.getValue(), entry.getKey());
			}
		}
		return result;
	}

	public static Map<String, Double> translate(double[] tp, double[][] phi,
			Vocabulary vocabulary, int limit) {
		Map<String, Double>[] topicMapArray = translate(phi, vocabulary, limit);
		double p = -1.0;
		int t = -1;
		for (int k = 0; k < tp.length; k++) {
			if (tp[k] > p) {
				p = tp[k];
				t = k;
			}
		}
		return topicMapArray[t];
	}

	/**
	 * To print the result in a well formatted form
	 * 
	 * @param result
	 */
	public static void explain(Map<String, Double>[] result,String fileName) {
		FileOutputStream fout = null;
		PrintWriter pw = null;
		try {
			fout = new FileOutputStream(
					MyConfig.LDAOutput+"/"+fileName);  //设置程序运行结果的文件存储位置 
			pw = new PrintWriter(new OutputStreamWriter(fout, "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		JSONArray ja=new JSONArray();
		JSONObject jo;
		for (Map<String, Double> topicMap : result) {
//			System.out.printf("topic %d :\n", i++);
//			pw.print("topic "+i+":\n");
			jo=new JSONObject();
			explain(topicMap,jo);
			//System.out.println();
			ja.put(jo);
			jo=null;
		}
		pw.print(ja.toString());

		try {
			pw.print("\n");
			pw.print("\r\n");
			pw.close();
			fout.close();

			pw = null;
			fout = null;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void explain(Map<String, Double> topicMap,JSONObject jo) {
		for (Map.Entry<String, Double> entry : topicMap.entrySet()) {
			System.out.println(entry);
			jo.put(entry.getKey(), entry.getValue());
//			if (pw != null) {
//				pw.print(entry + "\n");
//			}
		}
	}
}
